package com.three.crm.xpopup;

import android.content.Context;
import android.text.InputType;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;

import androidx.annotation.NonNull;

import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.BottomPopupView;
import com.lxj.xpopup.impl.LoadingPopupView;
import com.three.crm.R;
import com.three.crm.bean.AreaBean;
import com.three.crm.bean.CompanyBean;
import com.three.crm.constant.AppConstant;
import com.three.crm.http.CommonResultCallBack;
import com.three.crm.listener.OnXpopupDismissListener;
import com.three.crm.require.AppRequireService;
import com.three.crm.util.AppManager;
import com.three.crm.util.GsonUtils;
import com.three.crm.util.SignUtil;
import com.three.crm.util.TString;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

import okhttp3.Call;

import static com.three.crm.util.ToastUtils.showToast;

public class ReplyOrderPopup extends BottomPopupView {

    private int index;
    private int did;
    private String hintText;
    private int approveStatus = 1;

    private RadioGroup radioGroup;
    private EditText etContent;
    private Button btnSubmit;
    private Context mContent;
    private LoadingPopupView loadingView = null;

    private OnXpopupDismissListener onXpopupDismissListener;

    public void setOnXpopupDismissListener(OnXpopupDismissListener onXpopupDismissListener) {
        this.onXpopupDismissListener = onXpopupDismissListener;
    }

    public ReplyOrderPopup(@NonNull Context context) {
        super(context);
        this.mContent = context;
    }

    public ReplyOrderPopup(@NonNull Context context, int index, int did, String hintText) {
        super(context);
        this.index = index;
        this.did = did;
        this.hintText = hintText;
        this.mContent = context;
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.xpopup_reply;
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        loadingView = (LoadingPopupView) new XPopup.Builder(mContent)
                .asLoading("正在初始化...");
        radioGroup = findViewById(R.id.rg_approve);
        etContent = findViewById(R.id.et_content);
        etContent.setHint(hintText);
        if (index == 2) {
            etContent.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
            radioGroup.setVisibility(View.GONE);
        }
        btnSubmit = findViewById(R.id.btn_submit);
        btnSubmit.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                onSubmit();
            }
        });
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.rb_pass:
                        approveStatus = 1;
                        break;
                    case R.id.rb_nopass:
                        approveStatus = 2;
                        break;
                }
            }
        });
    }

    private void onSubmit() {
        String text = etContent.getText().toString();
        if (TextUtils.isEmpty(text)) {
            showToast("填写的内容不能为空！");
            return;
        }

        showLoading(mContent.getString(R.string.submitting));
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("userToken", AppManager.getToken());
            jsonObject.put("id",did);
            if (index == 1) {
                jsonObject.put("content",text);
                jsonObject.put("admin_status",approveStatus);
                jsonObject.put("timestamp",AppManager.getTimesTamp());
                String sign = SignUtil.genSign(jsonObject);
                jsonObject.put("sign",sign);
                Log.i("TAG","jsonObject="+jsonObject.toString());
                AppRequireService.replyOrder(mContent, AppConstant.FORM_SUBMIT, jsonObject, callBack);
            } else if (index == 2) {
                jsonObject.put("final_money",text);
                jsonObject.put("timestamp",AppManager.getTimesTamp());
                String sign = SignUtil.genSign(jsonObject);
                jsonObject.put("sign",sign);
                Log.i("TAG","jsonObject="+jsonObject.toString());
                AppRequireService.modifyFeedbackMoney(mContent, AppConstant.FORM_SUBMIT, jsonObject, callBack);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    private CommonResultCallBack callBack = new CommonResultCallBack(mContent) {
        @Override
        public void onResult(String result, int requireCode) {
            hideLoading();
            Log.i("TAG","result="+result);
            if (requireCode == AppConstant.FORM_SUBMIT) {
                smartDismiss();
                if (onXpopupDismissListener != null) {
                    onXpopupDismissListener.onXpopupDismiss();
                }
            }
        }

        @Override
        public void onConnectTimeOut(Call call, Exception e, int requireCode) {
            showToast(e.getMessage());
            hideLoading();
        }

        @Override
        public void onFail(String msg, int requireCode) {
            showToast(msg);
            hideLoading();
        }
    };

    protected void showLoading(String text){
        if (loadingView != null){
            loadingView.setTitle(text);
            loadingView.show();
        }
    }

    protected void hideLoading(){
        if (loadingView != null){
            loadingView.smartDismiss();
        }
    }
}
